<?php
declare (strict_types = 1);

namespace app\frontend\controller;

use app\BaseController;
use app\frontend\model\User;
use app\frontend\model\UserConfig;
use think\facade\Validate;

class Index extends BaseController
{
    // 控制器中间件
    // 登录检测
    protected array $middleware = ['app\frontend\middleware\IsLogin'];

    // 首页框架
    public function index()
    {
        $userInfo = session('userInfo');

        return view('', [
            'userInfo' => $userInfo
        ]);
    }

    // 首页页面
    public function home()
    {
        return view();
    }

    // 获取初始化数据
    public function getSystemInit()
    {
        $homeInfo = [
            'title' => '首页',
            'href'  => '/frontend/home/',
        ];
        $logoInfo = [
            'title' => '开发管理平台',
            'image' => '/static/frontend/images/logo.png',
            'href' => '/frontend/'
        ];
        $menuInfo = [
            [
                'title' => '开发管理',
                'icon' => 'fa fa-address-book',
                'href' => '',
                'target' => '_self',
                'child' => [
                    [
                        'title' => '项目',
                        'icon' => 'fa fa-lemon-o',
                        'href' => '/frontend/project',
                        'target' => '_self',
                        'child' => []
                    ],
                    [
                        'title' => '任务',
                        'icon' => 'fa fa-slideshare',
                        'href' => '/frontend/task',
                        'target' => '_self',
                        'child' => []
                    ],
                    [
                        'title' => '计划',
                        'icon' => 'fa fa-paper-plane',
                        'href' => '/frontend/plan',
                        'target' => '_self',
                        'child' => []
                    ],
                    [
                        'title' => '需求',
                        'icon' => 'fa fa-bookmark',
                        'href' => '/frontend/requirement',
                        'target' => '_self',
                        'child' => []
                    ]
                ]
            ],
            [
                'title' => '知识库',
                'icon' => 'fa fa-book',
                'href' => '',
                'target' => '_self',
                'child' => [
                    [
                        'title' => '文章',
                        'icon' => 'fa fa-book',
                        'href' => '',
                        'target' => '_self',
                        'child' => [
                            [
                                'title' => '所有文章',
                                'icon' => 'fa fa-bullhorn',
                                'href' => '/frontend/all-article',
                                'target' => '_self',
                                'child' => []
                            ],
                            [
                                'title' => '我的文章',
                                'icon' => 'fa fa-caret-square-o-up',
                                'href' => '/frontend/my-article',
                                'target' => '_self',
                                'child' => []
                            ]
                        ]
                    ],
                    [
                        'title' => '评论',
                        'icon' => 'fa fa-comments',
                        'href' => '',
                        'target' => '_self',
                        'child' => [
                            [
                                'title' => '发出的评论',
                                'icon' => 'fa fa-comment-o',
                                'href' => '/frontend/send-comment',
                                'target' => '_self',
                                'child' => []
                            ],
                            [
                                'title' => '收到的评论',
                                'icon' => 'fa fa-commenting-o',
                                'href' => '/frontend/get-comment',
                                'target' => '_self',
                                'child' => []
                            ]
                        ]
                    ],
                ]
            ]
        ];
        $systemInit = [
            'homeInfo' => $homeInfo,
            'logoInfo' => $logoInfo,
            'menuInfo' => $menuInfo,
        ];
        return json($systemInit);
    }

    // 基本资料
    public function basicInfo()
    {
        $id = (int)input('get.id', '', 'strip_tags');
        $basicInfo = User::find($id)->toArray();

        return view('', [
            'basicInfo' => $basicInfo,
        ]);
    }

    // 更新基本资料
    public function updateBasicInfo()
    {
        // 管理员信息
        $params['id'] = (int)trim(input('post.id', '', 'strip_tags'));
        $params['nickname'] =  trim(input('post.nickname', '', 'strip_tags'));
        $params['password'] = trim(input('post.password', '', 'strip_tags'));

        // 格式验证
        $validate = Validate::rule([
            'id|id' => 'require|integer',
            'password|密码' => 'max:20',
            'nickname|昵称' => 'require|max:20'
        ]);
        if (!$validate->check($params)) {
            return json([
                'code' => 1,
                'msg' => $validate->getError()
            ]);
        }

        // 处理密码
        if (empty($params['password'])) {
            unset($params['password']);
        } else {
            $params['password'] = password_hash($params['password'], PASSWORD_DEFAULT);
        }

        User::update($params);

        return json([
            'code' => 0,
            'msg' => '修改成功'
        ]);
    }

    // 个人配置
    public function personalConfig()
    {
        $userId = (int)input('get.id', '', 'strip_tags');
        $userConfig = UserConfig::where('user_id', $userId)->find();
        if (empty($userConfig)) {
            // 默认配置
            $config = [
                'format' => 'his',
                'hour' => 7,
                'minute' => 0
            ];
        } else {
            $config = json_decode($userConfig->config, true);
        }

        return view('', [
            'userConfig' => $config
        ]);
    }

    // 个人配置保存
    public function personalConfigSave()
    {
        $format = input('post.format', '', 'strip_tags');
        $hour = (int)input('post.hour', '', 'strip_tags');
        $minute = (int)input('post.minute', '', 'strip_tags');

        $userConfig = UserConfig::where('user_id', session('userInfo.id'))->find();
        if (empty($userConfig)) {
            $userConfig = new UserConfig();
        }
        $userConfig->user_id = session('userInfo.id');
        $userConfig->config = json_encode([
            'format' => $format,
            'hour' => $hour,
            'minute' => $minute
        ]);
        $userConfig->save();

        return json([
            'code' => 0,
            'msg' => '保存成功'
        ]);
    }
}
